<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="概述使用THREE.Points可以非常容易地创建很多细小的物体，用来模拟雨滴、雪花、烟等有趣效果，你也可以将整个几何体渲染成一组粒子，并分别控制。 THREE.Sprite(material)构造函数可以用于手工创建粒子，传入的唯一参数为材质。与THREE.Mesh一样，THREE.Sprite对象也是对THREE.Object3D的扩展；因此可以用position属性来定位，scale属性来进">
<meta property="og:type" content="article">
<meta property="og:title" content="three.js粒子">
<meta property="og:url" content="http://example.com/2021/10/03/webgl/three.js%E7%B2%92%E5%AD%90%E4%B8%8E%E7%B2%BE%E7%81%B5/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="概述使用THREE.Points可以非常容易地创建很多细小的物体，用来模拟雨滴、雪花、烟等有趣效果，你也可以将整个几何体渲染成一组粒子，并分别控制。 THREE.Sprite(material)构造函数可以用于手工创建粒子，传入的唯一参数为材质。与THREE.Mesh一样，THREE.Sprite对象也是对THREE.Object3D的扩展；因此可以用position属性来定位，scale属性来进">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-03T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-03T08:22:16.054Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="three.js">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/10/03/webgl/three.js%E7%B2%92%E5%AD%90%E4%B8%8E%E7%B2%BE%E7%81%B5/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>three.js粒子 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/10/03/webgl/three.js%E7%B2%92%E5%AD%90%E4%B8%8E%E7%B2%BE%E7%81%B5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          three.js粒子
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-10-03 12:18:39" itemprop="dateCreated datePublished" datetime="2021-10-03T12:18:39+08:00">2021-10-03</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:22:16" itemprop="dateModified" datetime="2021-11-03T16:22:16+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/webgl/" itemprop="url" rel="index"><span itemprop="name">webgl</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>使用THREE.Points可以非常容易地创建很多细小的物体，用来模拟雨滴、雪花、烟等有趣效果，你也可以将整个几何体渲染成一组粒子，并分别控制。</p>
<p>THREE.Sprite(material)构造函数可以用于手工创建粒子，传入的唯一参数为材质。与THREE.Mesh一样，THREE.Sprite对象也是对THREE.Object3D的扩展；因此可以用position属性来定位，scale属性来进行缩放，translate属性指定相对位移。</p>
<h2 id="Three-Points"><a href="#Three-Points" class="headerlink" title="Three.Points"></a>Three.Points</h2><p>利用THREE.Sprite来构建对象并移动很有效，但是当你想使用大量的该对象时，则会遇到性能问题，因为每个对象都需要THREE.js来进行管理，这里我们使用THREE.Points来做处理；这样只需要管理一个THREE.Points实例。</p>
<p>使用THREE.Points的代码如下：我们为每个粒子创建一个顶点（用THREE.Vector3表示），并将它添加到THREE.Geometry中，然后使用THREE.Geometry与THREE.PointsMaterial一起创建THREE.Points，再把云添加到场景中。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPoints</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> geom = <span class="keyword">new</span> THREE.Geometry();</span><br><span class="line">    <span class="keyword">var</span> material = <span class="keyword">new</span> THREE.PointsMaterial(&#123;</span><br><span class="line">      size: <span class="number">2</span>,</span><br><span class="line">      vertexColors: <span class="literal">true</span>,</span><br><span class="line">      color: <span class="number">0xffffff</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> x = <span class="number">-15</span>; x &lt; <span class="number">15</span>; x++) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">var</span> y = <span class="number">-10</span>; y &lt; <span class="number">10</span>; y++) &#123;</span><br><span class="line">        <span class="keyword">var</span> particle = <span class="keyword">new</span> THREE.Vector3(x * <span class="number">4</span>, y * <span class="number">4</span>, <span class="number">0</span>);</span><br><span class="line">        geom.vertices.push(particle);</span><br><span class="line">        geom.colors.push(<span class="keyword">new</span> THREE.Color(<span class="built_in">Math</span>.random() * <span class="number">0xffffff</span>));</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> cloud = <span class="keyword">new</span> THREE.Points(geom, material);</span><br><span class="line">    scene.add(cloud);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>THREE.Points的构造函数接收两个属性，几何体和材质；材质用于给粒子着色与添加纹理，而几何体则用于指定单个粒子的位置。每个顶点和每个用来定义几何体的点，将会以粒子的形态展示出来。如果我们基于THREE.BoxGeometry对象来创建THREE.Points对象，将会得到8个粒子，方块的每个角上各一个。</p>
<p>但一般情况下，我们都会选择使用THREE.PointsMaterial来样式化粒子。如下代码，我们创建了一个THREE.Points对象，并添加了15000个粒子，且所有粒子均由THREE.PointsMaterial样式化。</p>
<p>代码逻辑如下：1、创建一个THREE.Geometry对象；2、把用THREE.Vector3对象表示的粒子添加至几何体。3、定义颜色数组geom.colors。4、创建THREE.PointsMaterial。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createParticles</span>(<span class="params">size, transparent, opacity, vertexColors, sizeAttenuation, colorValue, vertexColorValue</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> geom = <span class="keyword">new</span> THREE.Geometry();</span><br><span class="line">    <span class="keyword">var</span> material = <span class="keyword">new</span> THREE.PointsMaterial(&#123;</span><br><span class="line">      size: size,</span><br><span class="line">      transparent: transparent,</span><br><span class="line">      opacity: opacity,</span><br><span class="line">      vertexColors: vertexColors,</span><br><span class="line"></span><br><span class="line">      sizeAttenuation: sizeAttenuation,</span><br><span class="line">      color: <span class="keyword">new</span> THREE.Color(colorValue)</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> range = <span class="number">500</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">15000</span>; i++) &#123;</span><br><span class="line">      <span class="keyword">var</span> particle = <span class="keyword">new</span> THREE.Vector3(<span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>, <span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>,</span><br><span class="line">        <span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>);</span><br><span class="line">      geom.vertices.push(particle);</span><br><span class="line">      <span class="keyword">var</span> color = <span class="keyword">new</span> THREE.Color(vertexColorValue);</span><br><span class="line">      <span class="keyword">var</span> asHSL = &#123;&#125;;</span><br><span class="line">      color.getHSL(asHSL);</span><br><span class="line">      color.setHSL(asHSL.h, asHSL.s, asHSL.l * <span class="built_in">Math</span>.random());</span><br><span class="line">      geom.colors.push(color);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cloud = <span class="keyword">new</span> THREE.Points(geom, material);</span><br><span class="line">    cloud.name = <span class="string">"particles"</span>;</span><br><span class="line">    scene.add(cloud);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>到目前为止，其实我们只是将粒子渲染成为小方块，这时粒子特效的默认行为，而其实还有其他更多的粒子效果实现方式，具体方式如下：</p>
<h2 id="HTML5画布"><a href="#HTML5画布" class="headerlink" title="HTML5画布"></a>HTML5画布</h2><p>THREE.js提供了三种使用HTML5画布来样式化粒子的方法。</p>
<h3 id="THREE-CanvasRenderer"><a href="#THREE-CanvasRenderer" class="headerlink" title="THREE.CanvasRenderer"></a>THREE.CanvasRenderer</h3><p>通过THREE.SpriteCanvasMaterial，可以将HTML5画布的输出结果作为粒子的纹理，该纹理仅有在使用THREE.CanvasRenderer时，才能够生效。具体的代码如下：由于我们使用的是THREE.CanvasRenderer，因此不使用THREE.Points，而是直接创建THREE.Sprite对象，且我们使用program属性来定义THREE.SpriteCanvasMaterial，program属性指向getTexture函数，该函数定义了粒子的外观。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createSprites</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> material = <span class="keyword">new</span> THREE.SpriteCanvasMaterial(&#123;</span><br><span class="line">      program: getTexture</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    material.rotation = <span class="built_in">Math</span>.PI;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> range = <span class="number">500</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">1500</span>; i++) &#123;</span><br><span class="line">      <span class="keyword">var</span> sprite = <span class="keyword">new</span> THREE.Sprite(material);</span><br><span class="line">      sprite.position.set(<span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>, <span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>, <span class="built_in">Math</span>.random() *</span><br><span class="line">        range - range / <span class="number">2</span>);</span><br><span class="line">      sprite.scale.set(<span class="number">0.1</span>, <span class="number">0.1</span>, <span class="number">0.1</span>);</span><br><span class="line">      scene.add(sprite);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> getTexture = <span class="function"><span class="keyword">function</span> (<span class="params">ctx</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// the body</span></span><br><span class="line">    ctx.translate(<span class="number">-81</span>, <span class="number">-84</span>);</span><br><span class="line">    ctx.fillStyle = <span class="string">"orange"</span>;</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.moveTo(<span class="number">83</span>, <span class="number">116</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">83</span>, <span class="number">102</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">83</span>, <span class="number">94</span>, <span class="number">89</span>, <span class="number">88</span>, <span class="number">97</span>, <span class="number">88</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">105</span>, <span class="number">88</span>, <span class="number">111</span>, <span class="number">94</span>, <span class="number">111</span>, <span class="number">102</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">111</span>, <span class="number">116</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">106.333</span>, <span class="number">111.333</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">101.666</span>, <span class="number">116</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">97</span>, <span class="number">111.333</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">92.333</span>, <span class="number">116</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">87.666</span>, <span class="number">111.333</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">83</span>, <span class="number">116</span>);</span><br><span class="line">    ctx.fill();</span><br><span class="line">    <span class="comment">// the eyes</span></span><br><span class="line">    ctx.fillStyle = <span class="string">"white"</span>;</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.moveTo(<span class="number">91</span>, <span class="number">96</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">88</span>, <span class="number">96</span>, <span class="number">87</span>, <span class="number">99</span>, <span class="number">87</span>, <span class="number">101</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">87</span>, <span class="number">103</span>, <span class="number">88</span>, <span class="number">106</span>, <span class="number">91</span>, <span class="number">106</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">94</span>, <span class="number">106</span>, <span class="number">95</span>, <span class="number">103</span>, <span class="number">95</span>, <span class="number">101</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">95</span>, <span class="number">99</span>, <span class="number">94</span>, <span class="number">96</span>, <span class="number">91</span>, <span class="number">96</span>);</span><br><span class="line">    ctx.moveTo(<span class="number">103</span>, <span class="number">96</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">100</span>, <span class="number">96</span>, <span class="number">99</span>, <span class="number">99</span>, <span class="number">99</span>, <span class="number">101</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">99</span>, <span class="number">103</span>, <span class="number">100</span>, <span class="number">106</span>, <span class="number">103</span>, <span class="number">106</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">106</span>, <span class="number">106</span>, <span class="number">107</span>, <span class="number">103</span>, <span class="number">107</span>, <span class="number">101</span>);</span><br><span class="line">    ctx.bezierCurveTo(<span class="number">107</span>, <span class="number">99</span>, <span class="number">106</span>, <span class="number">96</span>, <span class="number">103</span>, <span class="number">96</span>);</span><br><span class="line">    ctx.fill();</span><br><span class="line">    <span class="comment">// the pupils</span></span><br><span class="line">    ctx.fillStyle = <span class="string">"blue"</span>;</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.arc(<span class="number">101</span>, <span class="number">102</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="built_in">Math</span>.PI * <span class="number">2</span>, <span class="literal">true</span>);</span><br><span class="line">    ctx.fill();</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.arc(<span class="number">89</span>, <span class="number">102</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="built_in">Math</span>.PI * <span class="number">2</span>, <span class="literal">true</span>);</span><br><span class="line">    ctx.fill();</span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure>

<h3 id="WebGLRenderer"><a href="#WebGLRenderer" class="headerlink" title="WebGLRenderer"></a>WebGLRenderer</h3><p>如果我们想要在WebGLRenderer中使用HTML画布，可以才采取两种不同方法。1、使用THREE.PointsMaterial并创建THREE.Points对象；2、使用THREE.Sprite和THREE.SpriteMaterial的map属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createGhostTexture</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> canvas = <span class="built_in">document</span>.createElement(<span class="string">'canvas'</span>);</span><br><span class="line">    canvas.width = <span class="number">32</span>;</span><br><span class="line">    canvas.height = <span class="number">32</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> ctx = canvas.getContext(<span class="string">'2d'</span>);</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//第一个函数crreateGhostTexture中，我们基于H5的画布元素创建了一个THREE.Texture对象。</span></span><br><span class="line">    </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPoints</span>(<span class="params">size, transparent, opacity, sizeAttenuation, color</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> geom = <span class="keyword">new</span> THREE.Geometry();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> material = <span class="keyword">new</span> THREE.PointsMaterial(&#123;</span><br><span class="line">      size: size,</span><br><span class="line">      transparent: transparent,</span><br><span class="line">      opacity: opacity,</span><br><span class="line">      map: createGhostTexture(),</span><br><span class="line">      sizeAttenuation: sizeAttenuation,</span><br><span class="line">      color: color</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> range = <span class="number">500</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">5000</span>; i++) &#123;</span><br><span class="line">      <span class="keyword">var</span> particle = <span class="keyword">new</span> THREE.Vector3(<span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>, <span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>,</span><br><span class="line">        <span class="built_in">Math</span>.random() * range - range / <span class="number">2</span>);</span><br><span class="line">      geom.vertices.push(particle);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cloud = <span class="keyword">new</span> THREE.Points(geom, material);</span><br><span class="line">    cloud.name = <span class="string">'points'</span>;</span><br><span class="line">    scene.add(cloud);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//第二个函数中，我们将纹理赋给THREE.PointsMaterial的map属性。</span></span><br><span class="line">  <span class="comment">//上述函数的调用结果是：在createGhostTexture方法中绘制到画布上恶的所有内容都会用于THREE.points的粒子。</span></span><br></pre></td></tr></table></figure>

<h2 id="纹理样式化粒子"><a href="#纹理样式化粒子" class="headerlink" title="纹理样式化粒子"></a>纹理样式化粒子</h2><p>使用图像样式化粒子其实还有更直接的方法，可以使用THREE.TextureLoader().load()函数将图像加载为THREE.Texture对象，然后就可以将THREE.Texture分配给材质的map属性。</p>
<h2 id="使用精灵贴图"><a href="#使用精灵贴图" class="headerlink" title="使用精灵贴图"></a>使用精灵贴图</h2><h2 id="从高级几何体创建Points"><a href="#从高级几何体创建Points" class="headerlink" title="从高级几何体创建Points"></a>从高级几何体创建Points</h2>
    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/10/03/webgl/three.js%E7%B2%92%E5%AD%90%E4%B8%8E%E7%B2%BE%E7%81%B5/" title="three.js粒子">http://example.com/2021/10/03/webgl/three.js粒子与精灵/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/three-js/" rel="tag"># three.js</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/09/30/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%8E%92%E5%BA%8F/" rel="prev" title="查找与排序">
      <i class="fa fa-chevron-left"></i> 查找与排序
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/10/10/react/react%E5%B7%A5%E7%A8%8B%E5%B8%88%E4%BF%AE%E7%82%BC%E6%8C%87%E5%8D%97/" rel="next" title="react工程师修炼指南（一）">
      react工程师修炼指南（一） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Three-Points"><span class="nav-number">2.</span> <span class="nav-text">Three.Points</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HTML5画布"><span class="nav-number">3.</span> <span class="nav-text">HTML5画布</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#THREE-CanvasRenderer"><span class="nav-number">3.1.</span> <span class="nav-text">THREE.CanvasRenderer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#WebGLRenderer"><span class="nav-number">3.2.</span> <span class="nav-text">WebGLRenderer</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#纹理样式化粒子"><span class="nav-number">4.</span> <span class="nav-text">纹理样式化粒子</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用精灵贴图"><span class="nav-number">5.</span> <span class="nav-text">使用精灵贴图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#从高级几何体创建Points"><span class="nav-number">6.</span> <span class="nav-text">从高级几何体创建Points</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

